package com.carol.lmsbe.controller;

import com.carol.lmsbe.dto.StatisticsQueryRequest;
import com.carol.lmsbe.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 报表控制器
 */
@RestController
@RequestMapping("/api/reports")
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 导出设备统计报表
     */
    @GetMapping("/equipment/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public ResponseEntity<Resource> exportEquipmentReport(StatisticsQueryRequest request) {
        try {
            Resource resource = reportService.exportEquipmentReport(request);
            
            return ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .header(HttpHeaders.CONTENT_DISPOSITION, 
                            "attachment; filename=\"equipment_report.xlsx\"")
                    .body(resource);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 导出易耗品统计报表
     */
    @GetMapping("/consumable/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public ResponseEntity<Resource> exportConsumableReport(StatisticsQueryRequest request) {
        try {
            Resource resource = reportService.exportConsumableReport(request);
            
            return ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .header(HttpHeaders.CONTENT_DISPOSITION, 
                            "attachment; filename=\"consumable_report.xlsx\"")
                    .body(resource);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 导出设备使用报表
     */
    @GetMapping("/equipment/usage/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public ResponseEntity<Resource> exportEquipmentUsageReport(StatisticsQueryRequest request) {
        try {
            Resource resource = reportService.exportEquipmentUsageReport(request);
            
            return ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .header(HttpHeaders.CONTENT_DISPOSITION, 
                            "attachment; filename=\"equipment_usage_report.xlsx\"")
                    .body(resource);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 导出设备报废统计报表
     */
    @GetMapping("/equipment/scrap/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public ResponseEntity<Resource> exportEquipmentScrapReport(StatisticsQueryRequest request) {
        try {
            Resource resource = reportService.exportEquipmentScrapReport(request);
            
            return ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .header(HttpHeaders.CONTENT_DISPOSITION, 
                            "attachment; filename=\"equipment_scrap_report.xlsx\"")
                    .body(resource);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
